package com.itlong.cloud.app.service.impl;

import com.itlong.cloud.POJO.PO.PropertyParkCouponPO;
import com.itlong.cloud.POJO.VO.app.AppGetCouponListVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.app.dao.IAppCouponDao;
import com.itlong.cloud.app.service.IAppCouponService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      优惠券相关服务层接口实现类
 * <desc/>
 *
 * @createDate 2018/11/08
 */
@Service
public class AppCouponServiceImpl implements IAppCouponService {

    @Autowired
    private IAppCouponDao iAppCouponDao;

    /**
     * <desc>
     *      获取优惠券列表
     * </desc>
     *
     * @return 优惠券列表
     * @author wanghzi
     * @createDate 2018/11/09
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<AppGetCouponListVO> getCouponList(String phoneNo, String projectId, String redeemCode, String type) throws Exception {
        // 兼容公众号版本
        if (type == null) {
            type = "0";
        }
        return iAppCouponDao.getCouponList(phoneNo, projectId, redeemCode, type);
    }

    /**
     * <desc>
     *      获取优惠券信息 By 兑换码
     * </desc>
     *
     * @return 优惠券信息
     * @author wanghzi
     * @createDate 2018/11/09
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public PropertyParkCouponPO getCouponByRedeemCode(String redeemCode) throws Exception {
        return iAppCouponDao.getCouponByRedeemCode(redeemCode);
    }

    /**
     * <desc>
     *      添加(兑换)优惠券
     * </desc>
     *
     * @return 添加的优惠券
     * @author wanghzi
     * @createDate 2018/11/09
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public AppGetCouponListVO addCoupons(PropertyParkCouponPO propertyParkCouponPO) throws Exception {
        Map<String,Object> map = SqlUtil.durableData(propertyParkCouponPO, PlatformConstants.TABLE_UPDATE);
        iAppCouponDao.update(map);
        return iAppCouponDao.getCouponByCouponId(propertyParkCouponPO.getParkCouponId());
    }
}
